package com.zut.hospital.controller.pharmacy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.service.pharmacy.SaleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-06-21 14:37
 **/
@Controller
@RequestMapping("/hospital/sale")
public class SaleRecordController {
    @Autowired
    SaleRecordService saleRecordService;

    @ResponseBody
    @RequestMapping(value = "/all")

    public String show() {
        return saleRecordService.getAll();
}
    @ResponseBody
    @RequestMapping(value = "/shan")

    public String shan()throws ParseException {
        return saleRecordService.shan();
    }
    @ResponseBody
    @RequestMapping(value = "/zhe")

    public String zhe()throws ParseException {
        return saleRecordService.zhe(new Date());
    }
    @ResponseBody
    @RequestMapping(value = "/else")

    public String qita(@RequestBody String data,HttpSession session)throws ParseException {
        String medName=JSONObject.parseObject(data).getString("medName");
        String count=JSONObject.parseObject(data).getString("count");
        String other=JSONObject.parseObject(data).getString("other");
        int type=JSONObject.parseObject(data).getInteger("type");
        String user=session.getAttribute("adminUser").toString();

        return saleRecordService.saleelse(medName,count,other,user,type);
    }

    @ResponseBody
    @RequestMapping(value = "/nameClose")

    public String sale(@RequestBody String data,HttpSession session)throws ParseException {
        String user=session.getAttribute("adminUser").toString();
        JSONObject jsonObject = JSONObject.parseObject(data);
        String patname=JSONObject.parseObject(data).getString("patname");
        JSONArray jsonArray = jsonObject.getJSONArray("key");
        for (int i=0;i<jsonArray.size();i++){
            String conut = JSONObject.parseObject(jsonArray.getString(i)).getString("count");
            String name = JSONObject.parseObject(jsonArray.getString(i)).getString("name");
            saleRecordService.sale(name,conut,patname,user);
        }
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data","");
        return JSON.toJSONString(layuiMap);

    }
    @ResponseBody
    @RequestMapping(value = "/broken")

    public String broken(){
        return saleRecordService.broken();
    }
    @ResponseBody
    @RequestMapping(value = "/dingdan")

    public String dingdan(){
        return saleRecordService.dingdan();
    }
}
